﻿
Public Class u_NhanVien
    Dim DSNV1 As DataTable = New DataTable()
    Private Sub LoadData()
        Dim nv As New NhanVienBUS
        dgvDSNV.DataSource = nv.GetNhanVien
        Dim cv As New ChucVuBUS
        cbxChucVuNV.DataSource = cv.GetChucVu
        cbxChucVuNV.DisplayMember = "TenCV"
        cbxChucVuNV.ValueMember = "MaCV"
        Dim bc As New NhomBUS
        cbxBangCap.DataSource = bc.GetBangCap
        cbxBangCap.DisplayMember = "TenBangCap"
        cbxBangCap.ValueMember = "MaBangCap"
    End Sub
    Function kiemtra() As String
        If txtMaNV.Text = "" Or txtHoTenNV.Text = "" Or cbxBangCap.Text = "" Or txtDiaChiNV.Text = "" Or txtDienThoaiNV.Text = "" Or cbxChucVuNV.Text = "" Or txtCMNDNV.Text = "" Then
            MessageBox.Show("Không thể để trống thông tin")
            Return False
        End If
        Return True
    End Function
    Private Sub btnThemNV_Click(sender As Object, e As EventArgs) Handles btnThemNV.Click
        If kiemtra() = True Then
            Dim newNV As New NhanVienDTO
            newNV.MaNV = txtMaNV.Text
            newNV.TenNV = txtHoTenNV.Text
            newNV.NgaySinh = DateNV.Value.Date
            newNV.MaBangCap = cbxBangCap.SelectedValue
            newNV.DiaChi = txtDiaChiNV.Text
            newNV.DienThoai = txtDienThoaiNV.Text
            newNV.GioiTinh = rbNamNV.Checked
            newNV.ChucVu = cbxChucVuNV.SelectedValue
            newNV.CMND = txtCMNDNV.Text
            Dim nvBUS As New NhanVienBUS
            nvBUS.AddNhanVien(newNV)
            LoadData()
        End If
    End Sub

    Private Sub dgvDSNV_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dgvDSNV.SelectionChanged
        If dgvDSNV.SelectedRows.Count <> 0 Then
            If dgvDSNV.SelectedRows(0).Index <> dgvDSNV.RowCount Then
                txtMaNV.Text = dgvDSNV.SelectedRows(0).Cells("MaNV").Value
                txtHoTenNV.Text = dgvDSNV.SelectedRows(0).Cells("TenNV").Value
                cbxBangCap.SelectedValue = dgvDSNV.SelectedRows(0).Cells("MaBangCap").Value
                txtDiaChiNV.Text = dgvDSNV.SelectedRows(0).Cells("DiaChi").Value
                DateNV.Value = Date.Parse(dgvDSNV.SelectedRows(0).Cells("NgaySinh").Value)
                If dgvDSNV.SelectedRows(0).Cells("GioiTinh").Value = True Then
                    rbNamNV.Checked = True
                Else
                    rbNuNV.Checked = True
                End If
                txtDienThoaiNV.Text = dgvDSNV.SelectedRows(0).Cells("DienThoai").Value
                cbxChucVuNV.SelectedValue = dgvDSNV.SelectedRows(0).Cells("MaCV").Value
                txtCMNDNV.Text = dgvDSNV.SelectedRows(0).Cells("CMND").Value
            End If
        End If
    End Sub

    Private Sub btnXoaNV_Click(sender As Object, e As EventArgs) Handles btnXoaNV.Click
        If dgvDSNV.SelectedRows.Count <> 0 Then
            If dgvDSNV.SelectedRows(0).Index <> dgvDSNV.RowCount Then
                Dim delNV As New NhanVienDTO
                delNV.STT = dgvDSNV.SelectedRows(0).Cells("STT").Value
                Dim nvBUS As New NhanVienBUS
                nvBUS.DelNhanVien(delNV)
                LoadData()
            End If
        End If
    End Sub

    Private Sub btnCapNhapNV_Click(sender As Object, e As EventArgs) Handles btnCapNhapNV.Click
        If dgvDSNV.SelectedRows.Count <> 0 Then
            If dgvDSNV.SelectedRows(0).Index <> dgvDSNV.RowCount Then
                Dim newNV As New NhanVienDTO
                newNV.STT = dgvDSNV.SelectedRows(0).Cells("STT").Value
                newNV.MaNV = txtMaNV.Text
                newNV.TenNV = txtHoTenNV.Text
                newNV.NgaySinh = DateNV.Value.Date
                newNV.MaBangCap = cbxBangCap.SelectedValue
                newNV.DiaChi = txtDiaChiNV.Text
                newNV.DienThoai = txtDienThoaiNV.Text
                newNV.GioiTinh = rbNamNV.Checked
                newNV.ChucVu = cbxChucVuNV.SelectedValue
                newNV.CMND = txtCMNDNV.Text
                Dim nvBUS As New NhanVienBUS
                nvBUS.UpdateNhanVien(newNV)
                LoadData()
            End If
        End If
    End Sub

    Private Sub u_NhanVien_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DSNV1 = NhanVienBUS.Get_DSNV()
        dgvDSNV.DataSource = DSNV1
        LoadData()
        If FrmDangNhap.dto.MaNV = 1 Then
            btnCapNhapNV.Enabled = True
            btnLamMoi.Enabled = True
            btnThemNV.Enabled = True
            btnXoaNV.Enabled = True
        Else
            btnCapNhapNV.Enabled = False
            btnLamMoi.Enabled = False
            btnThemNV.Enabled = False
            btnXoaNV.Enabled = False
        End If
    End Sub

    Private Sub btnLamMoi_Click(sender As Object, e As EventArgs) Handles btnLamMoi.Click
        txtMaNV.Text = String.Empty
        cbxBangCap.Text = String.Empty
        txtHoTenNV.Text = String.Empty
        txtDiaChiNV.Text = String.Empty
        DateNV.Text = String.Empty
        cbxChucVuNV.Text = String.Empty
        txtDienThoaiNV.Text = String.Empty
        txtCMNDNV.Text = String.Empty
        'rbNamNV.Checked = String.Empty
        'rbNuNV.Checked = String.Empty
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim SaveFileDialog1 As SaveFileDialog = New SaveFileDialog()
        SaveFileDialog1.Filter = "Export (*.xls)|*.xls"
        SaveFileDialog1.ShowDialog()
        Dim File_name As String = SaveFileDialog1.FileName
        ExcelProvider.file_name = File_name
        Try

            For i As Integer = 0 To DSNV1.Rows.Count - 1
                Dim sql As String
                sql = String.Format("INSERT INTO [Sheet1$] (STT,MaNV,TenNV,NgaySinh,GioiTinh,MaBangCap,DienThoai,DiaChi,MaCV,CMND) values ('{0}', '{1}','{2}','{3}', '{4}','{5}','{6}', '{7}','{8}','{9}')", DSNV1.Rows(i)(0), DSNV1.Rows(i)(1), DSNV1.Rows(i)(2), DSNV1.Rows(i)(3), DSNV1.Rows(i)(4), DSNV1.Rows(i)(5), DSNV1.Rows(i)(6), DSNV1.Rows(i)(7), DSNV1.Rows(i)(8), DSNV1.Rows(i)(9))
                ExcelProvider.NonQuery(sql)
            Next
            MessageBox.Show("Xuất dữ liệu thành công")
        Catch ex As Exception
            MessageBox.Show("Xuất dữ liệu bị lỗi, bạn vui lòng kiểm tra lại cấu trúc file .xls")
        End Try
    End Sub
    Dim dt As DataTable = New DataTable()
    Private Sub btnNhap_Click(sender As Object, e As EventArgs) Handles btnNhap.Click
        MessageBox.Show("Bạn phải tạo ra file .xls  , trước khi thực hiên nếu không quá trình Import sẽ bị lỗi")

        Dim SaveFileDialog1 As SaveFileDialog = New SaveFileDialog()
        SaveFileDialog1.Filter = "Export (*.xls)|*.xls"
        SaveFileDialog1.ShowDialog()
        Dim File_name As String = SaveFileDialog1.FileName

        ExcelProvider.file_name = File_name

        Try
            Dim sql As String = "Select * From [Sheet1$]"
            dt = ExcelProvider.DataAdapter(sql)
            dgvDSNV.DataSource = dt

        Catch ex As Exception
            MessageBox.Show("Lỗi")
        End Try
    End Sub
End Class
